/**
 * 差旅审核业务逻辑处理实现类
 * @author 郑翎
 * 2015-10-1 11:07:43
 */
package com.cbts.service.info.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.cbts.dao.ApplayReimDao;
import com.cbts.dao.BusinessTravelListDao;
import com.cbts.dao.UserInfoDao;
import com.cbts.entity.ApplayReim;
import com.cbts.entity.BusinessTravelList;
import com.cbts.entity.UserInfo;
import com.cbts.service.info.VerifierService;
import com.cbts.service.travel.ApplayReimService;
import com.cbts.util.MailSender;
import com.cbts.util.SendEmail;
import com.opensymphony.xwork2.ActionContext;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

@ContextConfiguration("classpath:applicationContext.xml")
public class VerifierServiceImpl implements VerifierService {

	private BusinessTravelListDao businessTravelListdaoImpl; 	//出差列表数据处理类
	private ApplayReimService applayReimServiceImpl;			//报销的业务逻辑处理类
	private UserInfoDao userInfoDao;							//员工表数据处理类
	private ApplayReimDao applayReimDao;						//报销记录表示数据处理类
	
	/*
	 * (non-Javadoc)
	 * @see com.cbts.service.info.VerifierService#getBusinessTravelLists(java.lang.String)
	 */
	public List<BusinessTravelList> getBusinessTravelLists(String status) {

		try {
			
			String wherehql = "and verifierStatus=?";
			String[] objet = new String[1];
			objet[0] = status;
			List<BusinessTravelList> businessTravelLists = businessTravelListdaoImpl
					.findByProperty(wherehql, objet, -1, -1).getPageItems();
			
			return businessTravelLists;
		} catch (Exception e) {
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.cbts.service.info.VerifierService#alterStatus(java.lang.String, java.lang.String, com.cbts.entity.ApplayReim, java.lang.String)
	 */
	public String alterStatus(String travelOrder,String status,ApplayReim applayReim,String userid) {
		
		
		UserInfo userInfo = userInfoDao.get(userid);
		boolean b  = false, send = false,email = false;
		BusinessTravelList businessTravelList = businessTravelListdaoImpl.get(travelOrder);
		
		if (status.equals("已报销")) {
			
			System.err.println("travelOrder:"+applayReimDao.get(travelOrder).getTravelOrder());
			ApplayReim tempApplayReim = applayReimDao.get(travelOrder);
			tempApplayReim.setRealAppAmount(applayReim.getRealAppAmount());
			tempApplayReim.setSaveCost(applayReim.getSaveCost());
			applayReimDao.update(tempApplayReim);
			
		}
		
		businessTravelList.setVerifierStatus(status);
		businessTravelList.setUserInfoByVerifierId(userInfo);
		b= businessTravelListdaoImpl.update(businessTravelList);
		
		if (b==true && businessTravelList.getUserInfoByUserid().getEmail()!=null
				&&businessTravelList.getUserInfoByUserid().getEmailValidated().equals("是")) {
			
			email = true;
			//获取applicationContext
			ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
			MailSender mailSender = (MailSender)ac.getBean("mailSender");//获取MailSender实例
			
			StringBuffer validateString=new StringBuffer("您好！单号"+travelOrder+"的当前状态是："+status+"<br><br>");
			validateString.append("若这不是您本人要求的,请忽略本邮件,一切如常.<br><br>")
			.append("<a href=\"http://localhost:8080/BusinessTravelSystem/personTravelInfoAction!getApplayInfoList.action?travelorder=")
			.append(travelOrder)//单号
			.append("\">点击查看详情")
			.append("</a>")
			.append("<br><br><br><br>差旅管理系统");
			
			send = mailSender.send( businessTravelList.getUserInfoByUserid().getEmail(), "[差旅管理系统]状态跟踪",validateString.toString());
			

		}
		if (b==true&&send==true) {
			return "审核成功！已发送邮件通知员工！";
		}else if(b == false){
			return "提交审核失败！";
		}else if (b==true && send == false && email==true) {
			return "提交审核成功，邮件发送失败！请检查是否联网!";
			
		}else{
			return "提交审核成功，邮件发送失败！员工邮箱没有验证!";
		}
	}

	public BusinessTravelListDao getBusinessTravelListdaoImpl() {
		return businessTravelListdaoImpl;
	}

	public void setBusinessTravelListdaoImpl(
			BusinessTravelListDao businessTravelListdaoImpl) {
		this.businessTravelListdaoImpl = businessTravelListdaoImpl;
	}

	public ApplayReimService getApplayReimServiceImpl() {
		return applayReimServiceImpl;
	}

	public void setApplayReimServiceImpl(ApplayReimService applayReimServiceImpl) {
		this.applayReimServiceImpl = applayReimServiceImpl;
	}

	public UserInfoDao getUserInfoDao() {
		return userInfoDao;
	}

	public void setUserInfoDao(UserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}

	public ApplayReimDao getApplayReimDao() {
		return applayReimDao;
	}

	public void setApplayReimDao(ApplayReimDao applayReimDao) {
		this.applayReimDao = applayReimDao;
	}

}
